package cn.itcast.mq.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SpringRabbitListener {

    // @RabbitListener(queues = "demo.queue")
    // public void listenSimpleQueue(String msg) {
    //     //  既然这里报错了 如果尝试好几次 你都不成功 - 3次都是异常的，直接丢弃
    //     log.info("-------------接收到消息-----------");
    //     System.out.println(1 / 0);
    //     System.out.println("消费者接收到simple.queue的消息：【" + msg + "】");
    // }

    /**
     * 创建异常交换机和异常队列的绑定并且routingKey = error
     *
     * @param msg
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "error.queue"),
                    exchange = @Exchange(name = "error.direct"),
                    key = {"error"}
            ))
    public void listenErrorQueue(String msg) {
        log.info("------------收到了异常的信息: {}--------------", msg);
        // TODO 这里记录这个异常 或者进一步处理这个异常
    }

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue1(String msg) {
        log.info("--------------收到消息:{}", msg);
        System.out.println(1 / 0);
    }

    @RabbitListener(queues = "dlx.queue")
    public void listenDlxQueue1(String msg) {
        // 10s
        log.info("--------------死信消息:{}", msg);

    }

    @RabbitListener(queues = "delayed.queue")
    public void listenDelayedQueue(String msg) {
        log.info("--------------延迟消息:{}", msg);

    }

    @RabbitListener(queues = "lazy.queue")
    public void listenLazyQueue(String msg) {
        log.info("--------------listenLazyQueue:{}", msg);

    }

    @RabbitListener(queues = "ordinary.queue")
    public void listenOrdinaryQueue(String msg) {
        log.info("--------------listenOrdinaryQueue:{}", msg);

    }
}
